package com.glory.wxelectricitypda.base;

import android.os.Bundle;
import android.widget.EditText;

import com.glory.wxelectricitypda.bean.BaseResponseBean;
import com.glory.wxelectricitypda.bean.LoginBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.EditTextEnterUtils;
import com.glory.wxelectricitypda.utils.FreeApi_EditTextUtils;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author HX
 * @date 2019/12/5
 * @desc
 */
public class BaseDialogActivity extends FreeUI_BaseActivity {

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Constant.updateCurrentUser(getApplicationContext());
    }

    @Override
    protected void onResume() {
        super.onResume();
        Constant.updateCurrentUser(getApplicationContext());
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    protected void requestUser1(final String userName, final CallBack callBack) {
        JSONObject map = new JSONObject();
        try {
            map.put("USERNAME", userName);
            map.put("ACTIONTYPE", "CHECKUSERNAME");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestLogin(map, new FreeUI_DialogEntityCallBack<BaseResponseBean<LoginBean>>(
                new TypeToken<BaseResponseBean<LoginBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<LoginBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), false)) {
//                    Constant.CURRENT_USER = userName;
                    FreeApi_SharePreferencesUtils.setSharePre("URL_FILE", "USE_NAME", userName, getApplicationContext());
                    callBack.success();
                }else {
                    callBack.fail();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LoginBean>> response) {
                super.onError(response);
                callBack.error();
                loadError(response.getException(), "EUIT.EUITLOGIN");
            }
        });
    }

    protected void requestUser(final String userName, final CallBack callBack) {
        JSONObject map = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("USERNAME", userName);
            jsonObject1.put("ACTIONTYPE", "CHECKUSERNAME");
            map.put("Data",jsonObject1);
            map.put("MessageName","EUIT.EUITLOGIN");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(map, this,new FreeUI_DialogEntityCallBack<ResponseBean<LoginBean>>(
                new TypeToken<ResponseBean<LoginBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<LoginBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
//                    Constant.CURRENT_USER = userName;
                    FreeApi_SharePreferencesUtils.setSharePre("URL_FILE", "USE_NAME", userName, getApplicationContext());
                    callBack.success();
                }else {
                    callBack.fail();
                }
            }

            @Override
            public void onError(Response<ResponseBean<LoginBean>> response) {
                super.onError(response);
                callBack.error();
                loadError(response.getException(), "EUIT.EUITLOGIN");
            }
        });
    }

    public interface editTextListener{
        void onTextChangeListener(String s);
    }

    protected void initEdit(EditText editText, final editTextListener listener){
        EditTextEnterUtils.initEnterToUpperCase(editText);
        FreeApi_EditTextUtils.setEditTextListener(editText, this, s -> {
            if (listener != null){
                listener.onTextChangeListener(s);
            }
        });
    }

    public interface CallBack {
        void success();

        void fail();

        void error();

    }
}
